package org.lanplay.server.pages;

import javax.persistence.EntityManager;

import org.apache.wicket.Session;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.lanplay.server.ImplicationValidator;
import org.lanplay.server.LanPlaySession;
import org.lanplay.server.dao.UserDao;
import org.lanplay.server.entitymodel.EntityModel;
import org.lanplay.server.model.User;

import com.google.inject.Inject;
import com.google.inject.Provider;

public class UserProfilePage extends TransactionAwarePage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Inject
	private Provider<EntityManager> emp;

	@Inject
	private UserDao dao;

	public UserProfilePage() {
		add(new BookmarkablePageLink<WebPage>("linkToMain", WelcomePage.class));
		Form<User> form = new Form<User>("userForm",
				new CompoundPropertyModel<User>(new EntityModel<User>(emp,
						dao.getById((Long) Session.get().getAttribute(
								LanPlaySession.USER_ATTRIBUTE))))) {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				User user = getModelObject();
				dao.save(user);
			}

		};
		add(form);
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		form.add(new Label("key"));
		TextField<String> name = new TextField<String>("name");
		form.add(name);
		FormComponent<String> password = new PasswordTextField("password")
				.setRequired(false);
		form.add(password);
		// if password is set then username needs to be set too.
		form.add(new ImplicationValidator(password, name));
	}

}
